package com.googlecode.mad.mvntools.washotdeployer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import com.googlecode.mad.mvntools.common.Factory;
import com.googlecode.mad.mvntools.common.interfaces.IFileUtil;
import com.googlecode.mad.mvntools.common.interfaces.IMavenVersionUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * - Apache License 2.0 - <br>
 * Abstract Mojo to backup and restore a .x files from a project <br>
 * <br>
 * LastChangedBy: $Author: marthaler.worb@gmail.com $<br>
 * LastChangedDate: $Date: 2011-01-03 21:56:04 +0100 (Mon, 03 Jan 2011) $
 * 
 * @author daniel marthaler
 * @version $Revision: 27 $
 * @since 24.12.2010
 */
public abstract class AbstractHotdeployer extends AbstractMojo {

	/**
     * 
     */
	private static final String OWN_ARTIFACTID = "washotdeployer";
	/**
     * 
     */
	private static final String OWN_GROUPID = "com.googlecode.mad-mvntools";
	protected final static String FOLDER = "XXfolder:";
	protected final static String FILE = "XXfile:";
	protected final static String USER = "default";
	protected final static String USER_KEY = "user.name";
	protected final static String SEPARATOR_KEY = "file.separator";
	protected final static String TARGET = "target";
	protected final static String EMTYSTRING = "";

	protected String fileName = "abstractHotdeployer";

	protected XStream xstream = new XStream(new DomDriver());

	/**
	 * The project dependencies. *
	 * 
	 * @parameter expression="${project.build.directory} "
	 */
	protected String buildDir;

	/**
	 * The configured files
	 * 
	 * @parameter alias="wasRoot"
	 */
	private String websphereRoot;

	/**
	 * The configured folders
	 * 
	 * @parameter alias="earPath"
	 */
	private String pathToExpandedEar;

	/**
	 * The WasUser
	 * 
	 * @parameter expression="${user}" default-value="admin"
	 */
	protected String user;

	/**
	 * The WasPassword
	 * 
	 * @parameter expression="${password}" default-value="admin"
	 */
	protected String password;

	/** @parameter default-value="${project}" */
	protected org.apache.maven.project.MavenProject mavenProject;

	protected IFileUtil fileUtil = Factory.getInstance().getIFileUtil();

	protected IMavenVersionUtil mvnUtil = Factory.getInstance()
			.getIMavenVersionUtil();

	protected List<File> fileList = new ArrayList<File>();
	protected Set<File> folderList = new HashSet<File>();

	public void execute() throws MojoExecutionException {

		this.getLog().info(
				this.mvnUtil.getMavenString(AbstractHotdeployer.OWN_GROUPID,
						AbstractHotdeployer.OWN_ARTIFACTID, this.getClass()));
		this.prepareWebsphereEar();
		this.myExecute();

	}

	private void prepareWebsphereEar() {

		final IOFileFilter trueFilter = org.apache.commons.io.filefilter.TrueFileFilter.INSTANCE;
		getLog().info("EAR -> " + this.websphereRoot + this.pathToExpandedEar);
		final Collection<File> str = FileUtils.listFiles(new File(
				this.websphereRoot + this.pathToExpandedEar), trueFilter,
				trueFilter);

		for (final File file : str) {

			if (file.getAbsolutePath().contains(".war")) {
				final String[] folder = file.getAbsolutePath().split(".war");
				final File f = new File(folder[0] + ".war");
				this.folderList.add(f);
			} else if (file.getAbsolutePath().contains(".rar")) {
				final String[] folder = file.getAbsolutePath().split(".rar");
				final File f = new File(folder[0] + ".rar");
				this.folderList.add(f);
			} else {
				if (file.getName().endsWith(".jar")) {
					this.fileList.add(file);
				}
			}
		}
	}

	protected abstract void myExecute();

	protected String getRootPath() {
		return this.buildDir.replace(AbstractHotdeployer.TARGET,
				AbstractHotdeployer.EMTYSTRING);
	}

	protected File getFileForArtifact(final String artifact) {

		if (artifact.endsWith(".jar")) {
			for (final File file : this.fileList) {
				if (file.getAbsolutePath().endsWith(artifact)) {
					return file;
				}
			}
		} else {
			for (final File file : this.folderList) {
				if (file.getAbsolutePath().endsWith(artifact)) {
					return file;
				}
			}
		}
		return null;
	}

	protected void copyFileOrFolder(final File a, final File b) {
		if (a.isDirectory()) {
			try {
				FileUtils.cleanDirectory(b);
				FileUtils.copyDirectory(a, b);
			} catch (final IOException e) {
				this.getLog().error(e);
			}
		} else {
			try {
				FileUtils.copyFile(a, b);
			} catch (final IOException e) {
				this.getLog().error(e);
			}
		}
	}

}
